<?php
/**
 * Description of MenuViewGenerator
 *
 * @author Magno
 */
class MenuViewGenerator {
    
    private $tablesMeta;
    private $templateMenu;
    private $params;
    
    function __construct($tablesMeta, $params) {
        $this->tablesMeta = $tablesMeta;
        $this->params = $params;
        
        if(!file_exists(Constantes::$TEMPLATES_VIEW_DIR."menu.bloum"))
            throw new Exception ("Template Not Found!!!");

        $this->templateMenu = file_get_contents(Constantes::$TEMPLATES_VIEW_DIR."menu.bloum");
    }
    
    public function generate(){
        
        $menuActionDefault = '<li class="write" title=":Name"><a title="" href="#">:Name</a>
            <ul>
                <li><a title="Cadastrar" href=":NameAction.cadastro">Cadastrar</a></li>
                <li><a title="Listar" href=":NameAction.listar">Listar</a></li>                
            </ul>
        </li>';
        
        $menuAction = "";
        
        foreach ($this->tablesMeta as $tb) {
            if($tb->isCrud()){
                
                $menuActionTemp = $menuActionDefault;
                $menuActionTemp = str_replace(":Name", ucfirst($tb->getName()), $menuActionTemp)."\n\t\t";
                $menuAction .= $menuActionTemp;
                
            }
        }
        
        $this->templateMenu = str_replace(Constantes::$TPL_VIEW_MENU_ACTION, $menuAction, $this->templateMenu);
    }
    
    public function generateFile($generate = true){

        if($generate)
            $this->generate();

        $dirMenu = "temp_gen/".$this->params[Constantes::$PARAM_PROJECT_NAME]."/view/layout/";

        if(!file_exists($dirMenu))
            mkdir ($dirMenu, 0755,true);

        $fp = fopen($dirMenu."menu.tpl", "w+");
        fwrite($fp, $this->templateMenu);
        fclose($fp);
    }

}

?>
